﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;

namespace BAL
{
    public class UserLinkExtend : BaseBAL<UserLinkExtendInfo, Models.UserLinkExtend>
    {

        /// <summary>
        /// 以拼音缩写去的数据
        /// </summary>
        /// <param name="pinyin">拼音缩写</param>
        /// <param name="count">取几条</param>
        /// <returns></returns>
        public static IList<UserLinkExtendInfo> GetUserInfosByPinYin(string pinyin, int count)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                //return TransT2ToT1(GetT2IQueryable(t => t.PinYin.Contains(pinyin), db).ToList());
                if (count > 0)
                {
                    return db.GetTable<Models.UserLinkExtend>().Where(t => t.PinYin.Contains(pinyin)).Take(count).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return db.GetTable<Models.UserLinkExtend>().Where(t => t.PinYin.Contains(pinyin)).Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }
        public static IList<UserStatusInfo> GetUserDic(IList<int> UserId)
        {
            int[] ids = GetArray<int>.Get(UserId);
            using (var db = DatabaseHelper.GetDashboardData())
            {
                var query = db.UserLinkExtend.Where(u => ids.Contains(u.Id));
                //var queryable = db.UserLinkExtend.Where<UserLinkExtend>(Expression.Lambda<Func<UserLinkExtend, bool>>(Expression.Call(null, (MethodInfo) methodof(Enumerable.Contains), new Expression[] { Expression.Constant(ids), Expression.Property(expression = Expression.Parameter(typeof(UserLinkExtend), "t"), (MethodInfo) methodof(UserLinkExtend.get_Id)) }), new ParameterExpression[] { expression })).Select(Expression.Lambda(Expression.New((ConstructorInfo) methodof(<>f__AnonymousType9<string, string, string, int>..ctor, <>f__AnonymousType9<string, string, string, int>), new Expression[] { Expression.Property(expression = Expression.Parameter(typeof(UserLinkExtend), "t"), (MethodInfo) methodof(UserLinkExtend.get_Name)), Expression.Property(expression, (MethodInfo) methodof(UserLinkExtend.get_IdCard)), Expression.Property(expression, (MethodInfo) methodof(UserLinkExtend.get_Tel)), Expression.Property(expression, (MethodInfo) methodof(UserLinkExtend.get_Id)) }, new MethodInfo[] { (MethodInfo) methodof(<>f__AnonymousType9<string, string, string, int>.get_Name, <>f__AnonymousType9<string, string, string, int>), (MethodInfo) methodof(<>f__AnonymousType9<string, string, string, int>.get_IdCard, <>f__AnonymousType9<string, string, string, int>), (MethodInfo) methodof(<>f__AnonymousType9<string, string, string, int>.get_Tel, <>f__AnonymousType9<string, string, string, int>), (MethodInfo) methodof(<>f__AnonymousType9<string, string, string, int>.get_Id, <>f__AnonymousType9<string, string, string, int>) }), new ParameterExpression[] { expression }));
                IList<UserStatusInfo> list = new List<UserStatusInfo>();
                foreach (var type in query)
                {
                    UserStatusInfo item = new UserStatusInfo();
                    item.Id = type.Id;
                    item.IdCard = type.IdCard;
                    item.Name = type.Name;
                    item.Tel = type.Tel;
                    list.Add(item);
                }
                return list;
            }
        }


        /// <summary>
        /// 用身份证获取用户信息
        /// </summary>
        /// <param name="idcard">身份证</param>
        /// <returns></returns>
        public static UserLinkExtendInfo GetUserInfoByIdCard(string idcard)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return TransT2ToT1(db.UserLinkExtend.Single(t => t.IdCard == idcard));
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="Idcard">身份证号码</param>
        /// <param name="password">密码</param>
        /// <param name="userInfo">返回用户信息</param>
        /// <returns></returns>
        public static bool LoginUser(string Idcard, string password, ref UserLinkExtendInfo userInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Expression<Func<Models.UserLinkExtend, bool>>> funcs = new List<Expression<Func<Models.UserLinkExtend, bool>>>();
                funcs.Add(w => w.IdCard.ToLower() == Idcard.ToLower());
                funcs.Add(w => w.PassWord == password);
                var dd = GetT2IQueryable(funcs, db);
                if (dd.Count() > 0)
                {
                    userInfo = TransT2ToT1(dd.Single());
                    return true;
                }
                else
                    return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Idcard"></param>
        /// <param name="Tel"></param>
        /// <param name="first"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static IList<UserLinkExtendInfo> GetList(string Name, string Idcard, string Tel, int first, int max)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (max > 0)
                {
                    return db.GetTable<Models.UserLinkExtend>().Where(GetUserExpression(Name,Idcard,Tel)).Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return db.GetTable<Models.UserLinkExtend>().Where(GetUserExpression(Name, Idcard, Tel)).Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }

        /// <summary>
        /// 得到数据列表
        /// </summary>
        /// <param name="first">从第几条开始</param>
        /// <param name="max">读几条，=0时读取所有数据</param>
        /// <returns></returns>
        public static IList<UserLinkExtendInfo> GetList(int first, int max)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (max > 0)
                {
                    return db.GetTable<Models.UserLinkExtend>().Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return db.GetTable<Models.UserLinkExtend>().Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }

        /// <summary>
        /// 统计数量
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Idcard"></param>
        /// <param name="Tel"></param>
        /// <returns></returns>
        public static int GetCount(string Name, string Idcard, string Tel)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.UserLinkExtend>().Where(GetUserExpression(Name, Idcard, Tel)).Count();
            }
        }

        private static Expression<Func<Models.UserLinkExtend, bool>> GetUserExpression(string Name, string Idcard, string Tel)
        {
            var predicate = PredicateBuilder.True<Models.UserLinkExtend>();
            if (!PageValidate.IsEmpty(Name))
                predicate = predicate.And(p => p.Name == Name);
            if (!PageValidate.IsEmpty(Idcard))
                predicate = predicate.And(p => p.IdCard == Idcard);
            if (!PageValidate.IsEmpty(Tel))
                predicate = predicate.And(p => p.Tel == Tel);
            return predicate;
        }

        /// <summary>
        /// 得到一条用户信息
        /// </summary>
        /// <param name="IdCard">身份证号码</param>
        /// <param name="Tel">联系电话</param>
        /// <param name="Name">真实姓名</param>
        /// <returns></returns>
        public static UserLinkExtendInfo GetSingleInfo(string IdCard,string Tel,string Name)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    return TransT2ToT1(db.GetTable<Models.UserLinkExtend>().Single(t => t.IdCard == IdCard && t.Tel == Tel && t.Name == Name));
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 取得一条用户信息
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public static UserLinkExtendInfo GetSingleInfo(int Userid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    return TransT2ToT1(db.GetTable<Models.UserLinkExtend>().Single(t => t.Id == Userid));
                }
                catch
                {
                    return null;
                }
            }
        }
    }
}
